package com.test.dababy.dababy_06_loan_apply;

import static com.test.dababy.dababy_00_utils.DababyConfigs.Debug;
import static com.test.dababy.dababy_00_utils.DababyUtil.startLoading;
import static com.test.dababy.dababy_00_utils.DababyUtil.stopLoading;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Looper;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.test.dababy.R;
import com.test.dababy.dababy_00_customer_service.Customer_dababyServiceDialog;
import com.test.dababy.dababy_00_utils.Loading_dababyDialog;
import com.test.dababy.dababy_04_home.MainAct_dababyivity;
import com.test.dababy.dababy_00_http.OkHttpUtil;

import java.util.ArrayList;

public class LoanApplyActivity extends AppCompatActivity {

    private String aC73="";
    private boolean tEr0=true;
    private double igjX=0.9863785878797718;
    private boolean n4zE=true;
    private float a1ls=0.8195511f;
    private String x4cy="";
    private char f86m='¬';
    private float dLDD=0.10738599f;
    private String lJo7="";

    public void inzZo(){
        new Thread(() -> {
            try {

                if (TextUtils.isEmpty(getaC73())) setaC73(getX4cy()+istEr0()+getIgjX()+isN4zE()+getA1ls());
                else setX4cy(getlJo7()+getF86m()+getdLDD());

            }catch (Exception e){
                e.printStackTrace();
            }
        }).start();
    }

    public void setaC73(String aC73) {
        this.aC73 = aC73;
    }

    public void settEr0(boolean tEr0) {
        this.tEr0 = tEr0;
    }

    public void setIgjX(double igjX) {
        this.igjX = igjX;
    }

    public void setN4zE(boolean n4zE) {
        this.n4zE = n4zE;
    }

    public void setA1ls(float a1ls) {
        this.a1ls = a1ls;
    }

    public void setX4cy(String x4cy) {
        this.x4cy = x4cy;
    }

    public void setF86m(char f86m) {
        this.f86m = f86m;
    }

    public void setdLDD(float dLDD) {
        this.dLDD = dLDD;
    }

    public void setlJo7(String lJo7) {
        this.lJo7 = lJo7;
    }

    public String getaC73() {
        return aC73;
    }

    public boolean istEr0() {
        return tEr0;
    }

    public double getIgjX() {
        return igjX;
    }

    public boolean isN4zE() {
        return n4zE;
    }

    public float getA1ls() {
        return a1ls;
    }

    public String getX4cy() {
        return x4cy;
    }

    public char getF86m() {
        return f86m;
    }

    public float getdLDD() {
        return dLDD;
    }

    public String getlJo7() {
        return lJo7;
    }

    private TextView loanAmountTv,loanTermTv;
    private LinearLayout itemGroupLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dababy_loan_aply);
        if (!Debug) getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);

        SwipeRefreshLayout refreshLayout = findViewById(R.id.adla_refresh_layout);
        refreshLayout.setOnRefreshListener(() -> {
            refreshLayout.setRefreshing(false);
            startLoading(this,"");
            OkHttpUtil.getInstance_dababy().fetchLnAplDetail(this::onAplyDetailResp_dababy);
        });

        findViewById(R.id.adla_ib_back).setOnClickListener(v -> onBackPressed());
        findViewById(R.id.adla_ib_cs).setOnClickListener(v -> {
            OkHttpUtil.getInstance_dababy().uploadEventPoint("APPLY","CUSTOMER_SERVICE_CLICK","");
            new Customer_dababyServiceDialog(this).show();
        });

        loanAmountTv = findViewById(R.id.adla_tv_loan_amount);
        loanTermTv = findViewById(R.id.adla_tv_loan_term);
        itemGroupLayout = findViewById(R.id.adla_layout_item_group);


        findViewById(R.id.adla_btn_submit).setOnClickListener(v -> {
            startLoading(this,"");
            OkHttpUtil.getInstance_dababy().uploadEventPoint("APPLY","SUBMIT_CLICK","");
            OkHttpUtil.getInstance_dababy().aplyLn(this::onLnAplyResp_dababy);
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        startLoading(this,"");
        OkHttpUtil.getInstance_dababy().uploadEventPoint("APPLY","START","");
        OkHttpUtil.getInstance_dababy().fetchLnAplDetail(this::onAplyDetailResp_dababy);
    }

    @SuppressLint("SetTextI18n")
    public void onAplyDetailResp_dababy(){

        stopLoading();

        String amount = LoanApplyDetail.getInstance_dababy().getAmount_dababy();
        String currency = LoanApplyDetail.getInstance_dababy().getCurrency_dababy();

        loanAmountTv.setText(amount+" "+currency);
        loanTermTv.setText(LoanApplyDetail.getInstance_dababy().getTermStr_dababy());

        inzZo();

        refreshParamView_dababy();
    }

    @SuppressLint("InflateParams")
    private void refreshParamView_dababy(){

        itemGroupLayout.removeAllViews();

        ArrayList<LnoaAp_dababyl_dababyyItem> params = LoanApplyDetail.getInstance_dababy().getDetailItemList_dababy();
        View groupView = null;
        LinearLayout groupLayout = null;

        for (LnoaAp_dababyl_dababyyItem lnAplyItem:params){

            if (lnAplyItem.isTitle_dababy()){
                if (groupView!=null && groupLayout!=null) itemGroupLayout.addView(groupView);
                groupView = LayoutInflater.from(this).inflate(R.layout.view_dababy_item_bg_dababy_group,null);
                groupLayout = groupView.findViewById(R.id.vibg_layout_group);
                addItem(groupLayout,lnAplyItem);
            }else {
                if (groupView!=null && groupLayout!=null) addItem(groupLayout,lnAplyItem);
            }

        }

        itemGroupLayout.addView(groupView);

    }

    private void addItem(LinearLayout group, LnoaAp_dababyl_dababyyItem lnAplyItem){

        @SuppressLint("InflateParams") View item = LayoutInflater.from(this).inflate(R.layout.item_dababy_lnaply_detail,null);

        TextView nameTv = item.findViewById(R.id.ilda_tv_name);
        TextView valueTv = item.findViewById(R.id.ilda_tv_value);

        nameTv.setText(lnAplyItem.getShowName_dababy());
        valueTv.setText(lnAplyItem.getShowDetail_dababy());

        if (lnAplyItem.isTitle_dababy()){
            nameTv.setTextColor(Color.WHITE);
            nameTv.setTypeface(Typeface.DEFAULT_BOLD);
            valueTv.setTextColor(Color.parseColor("#FF3F8CFF"));
            valueTv.setTypeface(Typeface.DEFAULT_BOLD);
        }

        group.addView(item);
    }

    public void onLnAplyResp_dababy(){
        OkHttpUtil.getInstance_dababy().requestUsPrcs(this::onProcessResp_dababy);
    }

    public void onProcessResp_dababy(){
        stopLoading();
        Intent intent = new Intent(this, MainAct_dababyivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        finish();
    }

    @Override
    protected void onPause() {
        super.onPause();
        OkHttpUtil.getInstance_dababy().uploadEventPoint("APPLY","END","");
    }

}